﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SynoManager.GUI;

namespace SynoManager.DownloadStation.Ftp
{
    [DownloadStationPlugin("FTP Browser", "Browse and download from FTP-server", "Grad \"Flard\" van Horck", "1.0")]
    public class FtpGuiPlugin : IDownloadStationPlugin
    {
        private DownloadStationService service;

        public void Initialize(DownloadStationService service)
        {
            this.service = service;

            service.PluginConnected += new EventHandler(service_PluginConnected);
        }

        ToolStripItem[] items;
        void service_PluginConnected(object sender, EventArgs e)
        {
            Debug.Assert(items == null);
            
            items = new ToolStripItem[4];

            items[0] = new ToolStripSeparator();
            items[1] = new ToolStripButton("FTP Browser", Properties.Resources.server_compressed, openFtpBrowser);
            items[1].TextImageRelation = TextImageRelation.ImageAboveText;
            items[2] = new ToolStripSeparator();
            items[3] = new ToolStripMenuItem("FTP Browser", Properties.Resources.server_compressed,
                                             openFtpBrowser);

            service.Plugin.toolStrip.Items.Add(items[0]);
            service.Plugin.toolStrip.Items.Add(items[1]);
                
            service.Plugin.menuStrip.DropDownItems.Add(items[2]);
            service.Plugin.menuStrip.DropDownItems.Add(items[3]);
        }

        private void openFtpBrowser(object sender, EventArgs e)
        {
            IWin32Window owner = (IWin32Window) service.Plugin.Host;
            FtpConnectionDialog dialog = new FtpConnectionDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                FtpBrowserForm form = new FtpBrowserForm(service);
                form.Show();
                form.Connect(dialog.Address, dialog.Username, dialog.Password, dialog.UsePassive);
            }
        }

        public void Close()
        {
            service.Plugin.toolStrip.Items.Remove(items[0]);
            service.Plugin.toolStrip.Items.Remove(items[1]);
            service.Plugin.menuStrip.DropDownItems.Remove(items[2]);
            service.Plugin.menuStrip.DropDownItems.Remove(items[3]);
            items = null;
        }
    }
}
